package cn.sdormitory.basedata.service.impl;

import cn.sdormitory.basedata.dao.BDeviceDao;
import cn.sdormitory.basedata.dao.ClassDeviceDao;
import cn.sdormitory.basedata.dao.DorDeviceDao;
import cn.sdormitory.basedata.entity.BDevice;
import cn.sdormitory.basedata.entity.ClassDevice;
import cn.sdormitory.basedata.entity.DorDevice;
import cn.sdormitory.basedata.service.BDeviceService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @author: lx
 * @date: 2023/8/5 8:45
 * @description:
 */
@Service
public class BDeviceServiceImpl extends ServiceImpl<BDeviceDao, BDevice> implements BDeviceService {

    @Autowired
    BDeviceDao deviceDao;

    @Autowired
    ClassDeviceDao classDeviceDao;

    @Autowired
    DorDeviceDao dorDeviceDao;

    @Override
    public IPage<BDevice> findPageList(BDevice query) {
        Integer pageNum = query.getPageNum();
        Integer pageSize = query.getPageSize();
        int startNum = (pageNum - 1) * pageSize;
//        if(query.)
        int count = deviceDao.findPageListCount(query);
        List<BDevice> list = deviceDao.findPageList(startNum,pageSize,query);
        IPage<BDevice> page = new Page<>();
        page.setTotal(count);
        page.setRecords(list);
        return page;
    }

    @Override
    public int addOrUpdate(BDevice device) {
        if(device.getId() != null) {
            return this.baseMapper.updateById(device);
        }else {
            return this.baseMapper.insert(device);
        }
    }

    @Override
    public BDevice findById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    @Transactional
    public int bindClass(ClassDevice classDevice) {
        //当前设备存在绑定关系
        LambdaQueryWrapper<ClassDevice> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ClassDevice::getDeviceId,classDevice.getDeviceId());
        ClassDevice deviceIsExist = classDeviceDao.selectOne(wrapper1);
        if(deviceIsExist != null){
            return -1;
        }
        //当前班级存在绑定关系
        LambdaQueryWrapper<ClassDevice> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ClassDevice::getClassId,classDevice.getClassId());
        ClassDevice classIsExist = classDeviceDao.selectOne(wrapper2);
        if(classIsExist != null){
            return -1;
        }
        BDevice bDevice = deviceDao.selectById(classDevice.getDeviceId());
        //当前设备非教学楼设备，不可绑定班级
        if(!"2".equals(bDevice.getAddrType())){
            return -1;
        }

        //不存在绑定关系，插入数据关系
        int insert = classDeviceDao.insert(classDevice);
        //更新设备状态
        bDevice.setStatus("1");
        bDevice.setModifyTime(new Date());
        this.baseMapper.updateById(bDevice);
        return insert;
    }

    @Override
    public int bindDor(DorDevice dorDevice) {
        //查询当前设备是否存在绑定关系
        LambdaQueryWrapper<DorDevice> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(DorDevice::getDeviceId,dorDevice.getDeviceId());
        DorDevice deviceIsExist = dorDeviceDao.selectOne(wrapper1);
        if(deviceIsExist != null){
            return -1;
        }

        //当前楼层存在绑定关系
//        LambdaQueryWrapper<DorDevice> wrapper2 = new LambdaQueryWrapper<>();
//        wrapper2.eq(DorDevice::getBuildingNo,dorDevice.getBuildingNo())
//                .eq(DorDevice::getStorey,dorDevice.getStorey());
//        DorDevice dorIsExist = dorDeviceDao.selectOne(wrapper2);
//        if(dorIsExist != null){
//            return -1;
//        }

        BDevice bDevice = deviceDao.selectById(dorDevice.getDeviceId());
        //当前设备非宿舍设备，不可绑定宿舍
        if(!"1".equals(bDevice.getAddrType())){
            return -1;
        }
        //不存在绑定关系，插入数据关系
        int insert = dorDeviceDao.insert(dorDevice);
        //更新设备状态
        bDevice.setStatus("1");
        bDevice.setModifyTime(new Date());
        this.baseMapper.updateById(bDevice);
        return insert;
    }

    @Override
    @Transactional
    public int unbindClass(Long deviceId) {
        LambdaQueryWrapper<ClassDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClassDevice::getDeviceId,deviceId);
        int delete = classDeviceDao.delete(wrapper);
        if(delete == 0){
            return 9;
        }
        //更新设备状态
        BDevice bDevice = new BDevice();
        bDevice.setId(deviceId);
        bDevice.setStatus("2");
        bDevice.setModifyTime(new Date());
        this.baseMapper.updateById(bDevice);
        return delete;
    }

    @Override
    @Transactional
    public int unbindDor(Long deviceId) {
        LambdaQueryWrapper<DorDevice> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DorDevice::getDeviceId,deviceId);
        int delete = dorDeviceDao.delete(wrapper);
        if(delete == 0){
            return 9;
        }
        //更新设备状态
        BDevice bDevice = new BDevice();
        bDevice.setId(deviceId);
        bDevice.setStatus("2");
        bDevice.setModifyTime(new Date());
        this.baseMapper.updateById(bDevice);
        return delete;
    }

    @Override
    public void getDeviceInfoFromIotSys(List<Map<String, String>> deviceList) {
        List<BDevice> insertList = new ArrayList<>();
        for (Map<String, String> map : deviceList) {
            String serialNo = map.get("deviceCode");
            String deviceIp = map.get("deviceIp");
            String deviceAddr = map.get("deviceName");
            String addrType = map.get("attenceLocation");
            if(addrType.isEmpty()){
                addrType = "3";
            }
            LambdaQueryWrapper<BDevice> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BDevice::getSerialNo,serialNo);
            BDevice bDevice = deviceDao.selectOne(wrapper);
            if(bDevice != null){
                continue;
            }else {
                BDevice insert = new BDevice();
                insert.setSerialNo(serialNo);
                insert.setDeviceIp(deviceIp);
                insert.setDeviceAddr(deviceAddr);
                insert.setAddrType(addrType);
                insert.setStatus("2");
                insertList.add(insert);
            }
        }
        this.saveBatch(insertList);
    }

    @Override
    @Transactional
    public boolean delete(Map<String, String> map) {
        Long id = Long.valueOf(map.get("id"));
        String addrType = map.get("addrType");
        //宿舍类型考勤机
        if("1".equals(addrType)){
            LambdaQueryWrapper<DorDevice> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(DorDevice::getDeviceId,id);
            dorDeviceDao.delete(wrapper);
            this.baseMapper.deleteById(id);
            return true;
        }
        if("2".equals(addrType)){
            LambdaQueryWrapper<ClassDevice> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(ClassDevice::getDeviceId,id);
            classDeviceDao.delete(wrapper);
            this.baseMapper.deleteById(id);
            return true;
        }
        return false;
    }
}
